<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layout 管理界面大布局示例 - Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link href="//cdn.staticfile.org/layui/2.8.17/css/layui.css" rel="stylesheet">
    <style>
        .goods-list img {
            width: 50px;
        }
    </style>
</head>

<body>
    <div class="layui-layout layui-layout-admin">
        <div class="layui-header">
            <div class="layui-logo layui-hide-xs layui-bg-black">xxxx管理系统</div>
            <!-- 头部区域（可配合layui 已有的水平导航） -->
            <ul class="layui-nav layui-layout-left">
                <!-- 移动端显示 -->
                <li class="layui-nav-item layui-show-xs-inline-block layui-hide-sm" lay-header-event="menuLeft">
                    <i class="layui-icon layui-icon-spread-left"></i>
                </li>
                <li class="layui-nav-item layui-hide-xs"><a href="javascript:;">nav 1</a></li>
                <li class="layui-nav-item layui-hide-xs"><a href="javascript:;">nav 2</a></li>
                <li class="layui-nav-item layui-hide-xs"><a href="javascript:;">nav 3</a></li>
                <li class="layui-nav-item">
                    <a href="javascript:;">nav groups</a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;">menu 11</a></dd>
                        <dd><a href="javascript:;">menu 22</a></dd>
                        <dd><a href="javascript:;">menu 33</a></dd>
                    </dl>
                </li>
            </ul>
            <ul class="layui-nav layui-layout-right">
                <li class="layui-nav-item layui-hide layui-show-sm-inline-block">
                    <a href="javascript:;">
                        <img src="//unpkg.com/outeres@0.0.10/img/layui/icon-v2.png" class="layui-nav-img">
                        tester
                    </a>
                    <dl class="layui-nav-child">
                        <dd><a href="javascript:;">Your Profile</a></dd>
                        <dd><a href="javascript:;">Settings</a></dd>
                        <dd><a href="javascript:;">Sign out</a></dd>
                    </dl>
                </li>
                <li class="layui-nav-item" lay-header-event="menuRight" lay-unselect>
                    <a href="javascript:;">
                        <i class="layui-icon layui-icon-more-vertical"></i>
                    </a>
                </li>
            </ul>
        </div>
        <div class="layui-side layui-bg-black">
            <div class="layui-side-scroll">
                <!-- 左侧导航区域（可配合layui已有的垂直导航） -->
                <ul class="layui-nav layui-nav-tree" lay-filter="test">
                    <li class="layui-nav-item layui-nav-itemed">
                        <a class="" href="javascript:;">商品管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="./index.html">商品列表</a></dd>
                            <dd> <a href="./goods-send.html">发布商品</a></dd>
                            <dd class="layui-this"><a href="./goods-edit.html">编辑商品</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">轮播图管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">列表</a></dd>
                            <dd><a href="javascript:;">发布</a></dd>
                            <dd><a href="javascript:;">编辑</a></dd>
                        </dl>
                    </li>
                    <li class="layui-nav-item">
                        <a href="javascript:;">用户管理</a>
                        <dl class="layui-nav-child">
                            <dd><a href="javascript:;">列表</a></dd>
                        </dl>
                    </li>
                </ul>
            </div>
        </div>
        <div class="layui-body">
            <!-- 内容主体区域 -->
            <div style="padding: 15px;">

                <div class="layui-card layui-panel">
                    <div class="layui-card-header">

                    </div>
                    <div class="layui-card-body">

                        <div class="layui-form">
                            <input type="text" name="" placeholder="标题" class="layui-input title">
                            <input type="text" name="" placeholder="价格" class="layui-input price">
                            <hr class="ws-space-16">
                            <textarea name="" placeholder="详情" class="layui-textarea detail"></textarea>

                            <hr class="ws-space-16">


                            <button type="button" class="layui-btn" id="ID-upload-demo-btn">
                                <i class="layui-icon layui-icon-upload"></i> 单图片上传
                            </button>
                            <div style="width: 132px;">
                                <div class="layui-upload-list">
                                    <img class="layui-upload-img" id="ID-upload-demo-img"
                                        style="width: 100%; height: 92px;">
                                    <div id="ID-upload-demo-text"></div>
                                </div>
                                <div class="layui-progress layui-progress-big" lay-showPercent="yes"
                                    lay-filter="filter-demo">
                                    <div class="layui-progress-bar" lay-percent=""></div>
                                </div>
                            </div>


                            <hr class="ws-space-16">

                            <button type="button" class="layui-btn" onclick="saveFn()">
                                保存
                            </button>


                        </div>

                    </div>
                </div>
                <br><br>
            </div>
        </div>
        <div class="layui-footer">
            <!-- 底部固定区域 -->

        </div>
    </div>

    <script src="//cdn.staticfile.org/layui/2.8.17/layui.js"></script>
    <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
    <script src="./js/request.js"></script>
    <script src="./js/api.js"></script>
    <script>

        // 获取url中搜索参数 id  
        console.log(location.search);//?id=64ae66e7bb3cd9304d779820
        // 把搜索参数转为对象 {get(key){}}
        let obj = new URLSearchParams(location.search)
        //获取搜索参数中对应key的值
        let id = obj.get('id');
        console.log(id);
        layui.use(function () {
            if (!id) {
                //弹出提示 ，编辑页面只能从列表跳转过来
                layui.layer.msg('编辑页面只能从列表跳转过来')
                setTimeout(() => {
                    history.back()
                }, 1000);
            }
        })

        // 根据id请求商品的详情
        http.get(goodsAPI + '/' + id).then(r => {
            console.log(r.data);
            document.querySelector('.title').value = r.data.data.title
            document.querySelector('.price').value = r.data.data.price
            document.querySelector('.detail').value = r.data.data.content
            document.querySelector('#ID-upload-demo-img').src = r.data.data.coverImg
            imgUrl = r.data.data.coverImg
        })

        //JS 
        layui.use(['element', 'layer', 'util'], function () {
            var element = layui.element;
            var layer = layui.layer;
            var util = layui.util;
            var $ = layui.$;

            //头部事件
            util.event('lay-header-event', {
                menuLeft: function (othis) { // 左侧菜单事件
                    layer.msg('展开左侧菜单的操作', { icon: 0 });
                },
                menuRight: function () {  // 右侧菜单事件
                    layer.open({
                        type: 1,
                        title: '更多',
                        content: '<div style="padding: 15px;">处理右侧面板的操作</div>',
                        area: ['260px', '100%'],
                        offset: 'rt', // 右上角
                        anim: 'slideLeft', // 从右侧抽屉滑出
                        shadeClose: true,
                        scrollbar: false
                    });
                }
            });
        });


        let imgUrl


        // 上传图片
        layui.use(function () {
            var upload = layui.upload;
            var layer = layui.layer;
            var element = layui.element;
            var $ = layui.$;
            // 单图片上传
            var uploadInst = upload.render({
                field: 'img',
                elem: '#ID-upload-demo-btn',
                url: 'http://localhost:3000' + '/api/upload/img', // 实际使用时改成您自己的上传接口即可。
                before: function (obj) {
                    // 预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#ID-upload-demo-img').attr('src', result); // 图片链接（base64）
                    });

                    element.progress('filter-demo', '0%'); // 进度条复位
                    layer.msg('上传中', { icon: 16, time: 0 });
                },
                done: function (res) {
                    console.log(res);
                    imgUrl = res.imgUrl
                    // 若上传失败
                    if (res.code > 0) {
                        return layer.msg('上传失败');
                    }
                    // 上传成功的一些操作
                    // …
                    $('#ID-upload-demo-text').html(''); // 置空上传失败的状态
                },
                error: function () {
                    // 演示失败状态，并实现重传
                    var demoText = $('#ID-upload-demo-text');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                },
                // 进度条
                progress: function (n, elem, e) {
                    element.progress('filter-demo', n + '%'); // 可配合 layui 进度条元素使用
                    if (n == 100) {
                        layer.msg('上传完毕', { icon: 1 });
                    }
                }
            });
        })





        function saveFn() {
            layui.use(async function () {
                let $ = layui.$;
                let title = $('.title').val()
                let price = $('.price').val()
                let detail = $('.detail').val()

                console.log(imgUrl, title, price, detail);

                // 发请求
                let r = await http.patch(goodsAPI + '/' + id, { title, content: detail, coverImg: imgUrl, price })
                console.log(r);
                layui.layer.msg('保存成功')

                setTimeout(() => {
                    location.href = 'index.html'
                }, 1000);


            })
        }


    </script>
</body>

</html>